export function navigate(path: string, type?: string) {
	switch (type) {
		case 's':
			uni.switchTab({
				url: path,
			});
			break;
		case 'r':
			uni.redirectTo({
				url: path,
			});
			break;
		case 'b':
			uni.navigateBack();
			break;
		default:
			uni.navigateTo({
				url: path,
			});
	}
}

export function handleBack() {
	const pages = getCurrentPages();
	if (pages.length > 1) {
		uni.navigateBack({
			delta: 1,
		});
	} else {
		navigate('/pages/index/index', 's');
	}
}

//打开客服窗口
export function openCustomerServiceChat() {
	wx.openCustomerServiceChat({
		extInfo: {
			url: 'https://work.weixin.qq.com/kfid/kfc8e3136ffb859fe9d',
		},
		corpId: 'ww8ffa21734ec30d91',
		success(res) {
			console.log(res);
		},
		fail(err) {
			console.log(err);
		},
	});
}

//拨打电话
export function makePhoneCall(phone: string) {
	let arr = phone.split(',').map(item => {
		return {
			name: item,
		};
	});
	if (arr.length === 1) {
		uni.makePhoneCall({
			phoneNumber: phone,
			fail(err) {
				console.log(err);
			},
		});
		return Promise.resolve(false);
	} else {
		return Promise.resolve(arr);
	}
}

type queryType = {
	latitude: number;
	longitude: number;
	name: string;
	address: string;
};
//导航
export function openLocation(query: queryType) {
	let { latitude, longitude, name, address } = query;
	uni.openLocation({
		latitude: Number(latitude),
		longitude: Number(longitude),
		name,
		address,
		success: function () {
			console.log('success');
		},
		fail(err) {
			console.log(err);
		},
	});
}
